package com.se.frontline.gameplay.units;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;

import com.se.frontline.gameplay.ManagedGameScene;
import com.se.frontline.managers.ResourceManager;

public class Unit1 extends Unit {

	private AnimatedSprite Unit1Sprite;
	private MoveXModifier move;
	public float Height;
	public float Width;
	private boolean isEnemy;

	// ====================================================
	// Statistics of this Unit
	// ====================================================

	public int Health=104;
	private int Damage;
	private int Armor;

	public Unit1(boolean pIsEnemy){

		isEnemy = pIsEnemy;
		if(isEnemy==false){
		Unit1Sprite = new AnimatedSprite(-50, 0, ResourceManager.getInstance().mUnit1TextureRegion,
						ResourceManager.getInstance().engine.getVertexBufferObjectManager());}
		if(isEnemy==true){
			Health =300;
			Unit1Sprite = new AnimatedSprite(-50, 0, ResourceManager.getInstance().mUnit1TextureRegion,
							ResourceManager.getInstance().engine.getVertexBufferObjectManager());
			Unit1Sprite.setFlippedHorizontal(isEnemy);}
			
				Unit1Sprite.animate(300);
				
				this.Height = Unit1Sprite.getHeight();
				this.Width = Unit1Sprite.getWidth();

	}

	@Override
	public void go(float pY, Unit myself) {
		Unit1Sprite.setY(pY);
		if(Unit1.this.isEnemy==false){move = new MoveXModifier(5, -50, 300);}
		else if(Unit1.this.isEnemy==true){move = new MoveXModifier(15, 1650, -100);}
		Unit1Sprite.registerEntityModifier(move);
		ResourceManager.getInstance().getGameGround().attachChild(Unit1Sprite);
		Unit1Sprite.setUserData(myself);
		move.isAutoUnregisterWhenFinished();
		if(move.isFinished()==true){Unit1Sprite.detachSelf();}
		
	}

	@Override
	public float getHeight() {
		return Height;
	}
	@Override
	public Sprite getSprite() {
		return Unit1Sprite;
	}
	
	public void fight(final Unit enemy)
	{
		Unit1Sprite.unregisterEntityModifier(move);
		Unit1Sprite.animate(1);
		Thread timer = new Thread() {
			public void run() {
				boolean victory=false;

					try {
						sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						
						
						if(enemy.Health<=0){
						
						//Unit1Sprite.detachSelf();
						//enemy.getSprite().registerEntityModifier(move);
						victory=true;
						}
						
						System.out.println(enemy.Health);
						//System.out.println(enemy.Health);
						if(victory==true){
							enemy.getSprite().detachSelf();
							
Unit1Sprite.registerEntityModifier(move);
							Unit1Sprite.animate(300);
						}
						enemy.Health--;
						//System.out.println("3333");

					}
					
			}
		};

		timer.start();
	
	}

}
